package com.yuzhi.master.drainStPptnr.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuzhi.common.contant.Constants;
import com.yuzhi.common.core.page.PageDataInfo;
import com.yuzhi.master.drainStPptnr.domain.bo.DrainStPptnRDayBo;
import com.yuzhi.master.drainStPptnr.domain.vo.DrainStPptnRDayVo;
import com.yuzhi.master.drainStPptnr.mapper.DrainStPptnRDayMapper;
import com.yuzhi.master.drainStPptnr.service.IDrainStPptnRDayService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 降水量统计(日表) 服务实现
 */
@RequiredArgsConstructor
@Service
public class DrainStPptnRDayServiceImpl implements IDrainStPptnRDayService {

    private final DrainStPptnRDayMapper drainStPptnRDayMapper;

    @Override
    public PageDataInfo<DrainStPptnRDayVo> listDayData(DrainStPptnRDayBo bo) {
        // 构建分页对象
        Page<DrainStPptnRDayVo> result = drainStPptnRDayMapper.listDayData(
                bo.getPageQuery().build(), // 使用 PageQuery 构建 Page
                buildQueryWrapper(bo)
        );
        return PageDataInfo.build(result);
    }

    /**
     * 构建查询条件 Wrapper
     *
     * @param request BO 请求对象
     * @return 查询条件包装
     */
    private Wrapper<DrainStPptnRDayBo> buildQueryWrapper(DrainStPptnRDayBo request) {
        QueryWrapper<DrainStPptnRDayBo> wrapper = Wrappers.query();

        // 仅查询未删除数据
        wrapper.eq("dsprd.deleted", Constants.UNDELETED);

        // 按时间范围查询 tm 字段
        if (request.getStartTime() != null && request.getEndTime() != null) {
            wrapper.between("dsprd.tm", request.getStartTime(), request.getEndTime());
        }

        return wrapper;
    }

    /**
     * 不分页查询日表数据
     *
     * @param bo 查询条件（含时间范围）
     * @return 包含所有数据的结果对象
     */
    @Override
    public PageDataInfo<DrainStPptnRDayVo> DayListAll(DrainStPptnRDayBo bo) {
        // 调用mapper查询所有符合条件的数据
        List<DrainStPptnRDayVo> dataList = drainStPptnRDayMapper.listAllDayData(buildQueryWrapper(bo));

        // 构建分页信息对象，保持返回格式一致
        PageDataInfo<DrainStPptnRDayVo> pageData = new PageDataInfo<>();
        pageData.setList(dataList);
        pageData.setTotalPage(dataList.size());
        pageData.setPageNum(1);
        pageData.setPageSize(dataList.size());

        return pageData;
    }
}